//
//  ClubEditBtnCell.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/8/31.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ClubEditBtnCell.h"
#import <UIImageView+WebCache.h>
#import "YYText.h"
#import "YYImage.h"
#import "NSAttributedString+YYText.h"


#define TextFont 16
#define LineSpace 8
#define KernSpace 3
@interface ClubEditBtnCell ()

@property (nonatomic,weak) YYLabel *yyLab;
@property (nonatomic,strong) NSMutableArray *images;

@end

@implementation ClubEditBtnCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        self.images = [NSMutableArray array];
        for (NSInteger i=0; i<3; i++) {
            UIImageView *image = [[UIImageView alloc]init];
            [self.contentView addSubview:image];
            [self.images addObject:image];
        }
        
        YYLabel *yyLab = [[YYLabel alloc]init];
        yyLab.numberOfLines = 0;
        yyLab.preferredMaxLayoutWidth = ScreenW-20;
        self.yyLab = yyLab;
        [self.contentView addSubview:yyLab];
    }
    return self;
}
- (IBAction)editClick:(UIButton *)btn
{
    if (self.model) {
        if (btn.tag == 1) {
            if (self.editBlock) {
                self.editBlock(self.model);
            }
        }else if (btn.tag == 2) {
            if (self.deleteBlock) {
                self.deleteBlock(self.model);
            }
        }
    }
    if (self.oldModel) {
        if (btn.tag == 1) {
            if (self.editOldBlock) {
                self.editOldBlock(self.oldModel);
            }
        }else if (btn.tag == 2) {
            if (self.deleteOldBlock) {
                self.deleteOldBlock(self.oldModel);
            }
        }
    }
}
- (void)setModel:(ImageTitleModel *)model
{
    _model = model;
    for (NSInteger i=0; i<3; i++) {
        UIImageView *image = self.images[i];
        image.height = 0;
    }
    for (NSInteger i=0;i<model.clubImageList.count;i++) {
        UIImageView *image = self.images[i];
        [image sd_setImageWithURL:[NSURL URLWithString:model.clubImageList[i]] placeholderImage:GetImage(@"default_large_goods_image")];
    }
    if (!model.clubContent) return;
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:model.clubContent];
    attri.yy_font = [UIFont systemFontOfSize:TextFont];
    attri.yy_color = [UIColor blackColor];
    attri.yy_alignment = NSTextAlignmentJustified;
    attri.yy_lineSpacing = LineSpace;
    attri.yy_kern = @(KernSpace);
    
    //计算文本尺寸
    CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, MAXFLOAT);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:attri];
    self.yyLab.textLayout = layout;
    self.yyLab.attributedText = attri;
}
- (void)setOldModel:(OldImageDetail *)oldModel
{
    _oldModel = oldModel;
    for (NSInteger i=0; i<3; i++) {
        UIImageView *image = self.images[i];
        image.height = 0;
    }
    for (NSInteger i=0;i<oldModel.dynamicImageList.count;i++) {
        UIImageView *image = self.images[i];
        [image sd_setImageWithURL:[NSURL URLWithString:oldModel.dynamicImageList[i]] placeholderImage:GetImage(@"default_large_goods_image")];
    }
    if (!oldModel.dynamicImageContent) return;
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:oldModel.dynamicImageContent];
    attri.yy_font = [UIFont systemFontOfSize:TextFont];
    attri.yy_color = [UIColor blackColor];
    attri.yy_alignment = NSTextAlignmentJustified;
    attri.yy_lineSpacing = LineSpace;
    attri.yy_kern = @(KernSpace);
    
    //计算文本尺寸
    CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, MAXFLOAT);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:attri];
    self.yyLab.textLayout = layout;
    self.yyLab.attributedText = attri;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    if (self.model) {
        CGFloat maxY = 40;
        for (NSInteger i=0; i<self.model.clubImageList.count; i++) {
            UIImageView *imageView = self.images[i];
            UIImage *image = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:self.model.clubImageList[i]];
            CGFloat imageH = [self getImageH:image];
            imageView.height = imageH;
            imageView.frame = CGRectMake(10, maxY, ScreenW-20, imageH);
            maxY += imageH+5;
        }
        self.yyLab.frame = CGRectMake(10, maxY+5, ScreenW-20, [self getTextH:self.model.clubContent]);
    }
    if (self.oldModel) {
        CGFloat maxY = 40;
        for (NSInteger i=0; i<self.oldModel.dynamicImageList.count; i++) {
            UIImageView *imageView = self.images[i];
            UIImage *image = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:self.oldModel.dynamicImageList[i]];
            CGFloat imageH = [self getImageH:image];
            imageView.height = imageH;
            imageView.frame = CGRectMake(10, maxY, ScreenW-20, imageH);
            maxY += imageH+5;
        }
        self.yyLab.frame = CGRectMake(10, maxY+5, ScreenW-20, [self getTextH:self.oldModel.dynamicImageContent]);
    }
}

- (CGFloat)calculateDetailCellHWithModel:(ImageTitleModel *)model
{
    CGFloat maxY = 40;
    for (NSInteger i=0; i<model.clubImageList.count; i++) {
        UIImage *image = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:model.clubImageList[i]];
        CGFloat imageH = [self getImageH:image];
        maxY += imageH+5;
    }
    maxY += [self getTextH:model.clubContent]+15;
    return maxY;
}
- (CGFloat)calculateOldDetailCellHWithModel:(OldImageDetail *)model
{
    CGFloat maxY = 40;
    for (NSInteger i=0; i<model.dynamicImageList.count; i++) {
        UIImage *image = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:model.dynamicImageList[i]];
        CGFloat imageH = [self getImageH:image];
        maxY += imageH+5;
    }
    maxY += [self getTextH:model.dynamicImageContent]+15;
    return maxY;
}

- (CGFloat)getTextH:(NSString *)str
{
    if (!str) return 0;
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:str];
    attri.yy_font = [UIFont systemFontOfSize:TextFont];
    attri.yy_color = [UIColor blackColor];
    attri.yy_lineSpacing = LineSpace;
    attri.yy_kern = @(KernSpace);
    
    CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width-40, MAXFLOAT);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:attri];
    return layout.textBoundingSize.height;
}
- (CGFloat)getImageH:(UIImage *)image
{
    if (image) {
        CGFloat imageH = (ScreenW-20)*image.size.height/image.size.width*1.0;
        return imageH;
    }
    return 200;
}

@end
